package com.yanqu.road.dao.impl.activity.busyfarming;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.busyfarming.BusyFarmingUserReward;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class BusyFarmingUserRewardDaoImpl extends TempDataObjectDao<BusyFarmingUserReward> {


    public long getMaxUserRewardDbId(int activityId) {
        String sql = "select ifnull(max(dbId),0) as maxId from t_u_activity_busy_farming_user_reward where activityId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }


    public static String getBusyFarmingUserRewardSql = "select * from t_u_activity_busy_farming_user_reward where activityId=? and del=0";
    public Map<Long, List<BusyFarmingUserReward>> getBusyFarmingUserReward(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, List<BusyFarmingUserReward>> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getBusyFarmingUserRewardSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                BusyFarmingUserReward data = parse(rs);
                if(!result.containsKey(data.getUserId())){
                    result.put(data.getUserId(), new ArrayList<>());
                }
                result.get(data.getUserId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertBusyFarmingUserRewardSql = "insert into t_u_activity_busy_farming_user_reward(dbId,userId,activityId,friendId,helpType,params,getReward,reward,del,addTime) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(BusyFarmingUserReward data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getDbId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getFriendId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpType()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getParams(), "@#$")));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddTime()));
        if (getDb().executeNoneQuery(insertBusyFarmingUserRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateBusyFarmingUserRewardSql = "update t_u_activity_busy_farming_user_reward set friendId=?,helpType=?,params=?,getReward=?,reward=?,del=?,addTime=? where  activityId=? and dbId=? and userId=?";

    @Override
    protected boolean updateData(BusyFarmingUserReward data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getFriendId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getHelpType()));
        para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(data.getParams(), "@#$")));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isGetReward()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getReward()));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDel()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAddTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDbId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateBusyFarmingUserRewardSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected BusyFarmingUserReward parseData(ResultSet rs) throws SQLException {
        BusyFarmingUserReward busyFarmingUserReward = new BusyFarmingUserReward();
        busyFarmingUserReward.setDbId(rs.getLong("dbId"));
        busyFarmingUserReward.setUserId(rs.getLong("userId"));
        busyFarmingUserReward.setActivityId(rs.getInt("activityId"));
        busyFarmingUserReward.setFriendId(rs.getLong("friendId"));
        busyFarmingUserReward.setHelpType(rs.getInt("helpType"));
        busyFarmingUserReward.setParams(StringUtils.stringToStringList(rs.getString("params"), "@#\\$"));
        busyFarmingUserReward.setGetReward(rs.getBoolean("getReward"));
        busyFarmingUserReward.setReward(rs.getString("reward"));
        busyFarmingUserReward.setDel(rs.getBoolean("del"));
        busyFarmingUserReward.setAddTime(rs.getInt("addTime"));

        return busyFarmingUserReward;
    }
}
